package com.dongdongshop.serviceimpl;

import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.PageUtil;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemExample;
import com.dongdongshop.service.TbItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbItemServiceImpl implements TbItemService {
    @Autowired
    private TbItemMapper tbItemMapper;
    //查询所有商品
    @Override
    public PageUtil findallItem(String auditStatus, String goodsName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbGoods> list = tbItemMapper.selectFindall(auditStatus,goodsName);
        PageInfo<TbGoods> page = new PageInfo<>(list);
        PageUtil page2 = new PageUtil();
        page2.setPageNum(page.getPageNum());
        page2.setPages(page.getPages());
        page2.setPageSize(page.getPageSize());
        page2.setList(page.getList());
        return page2;
    }

    //商家后台查询所有商品
    @Override
    public PageUtil findallItem2(String auditStatus, String goodsName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbGoods> list = tbItemMapper.selectFindall2(auditStatus,goodsName);
        PageInfo<TbGoods> page = new PageInfo<>(list);
        PageUtil page2 = new PageUtil();
        page2.setPageNum(page.getPageNum());
        page2.setPages(page.getPages());
        page2.setPageSize(page.getPageSize());
        page2.setList(page.getList());
        return page2;
    }

    //根据goodsid查询item数据
    @Override
    public List<TbItem> selectItemByGoodsId(Long goodsid) {
        TbItemExample te = new TbItemExample();
        TbItemExample.Criteria criteria = te.createCriteria();
        criteria.andStatusEqualTo("1");
        criteria.andNumGreaterThan(0);
        criteria.andGoodsIdEqualTo(goodsid);
        te.setOrderByClause(" is_default desc ");
        List<TbItem> list = tbItemMapper.selectByExample(te);
        return list;
    }

    //1.根据商品id查询到商品
    @Override
    public TbItem selectItemById(Long itemId) {
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        return tbItem;
    }
}
